﻿using System;
using System.Collections.Generic;
using IupNET;
using System.Runtime.InteropServices;
using System.Threading;


namespace ACTKTest
{
    static class Program
    {
        static Dialog d;
        static Control page1, page2, page3;
        static ZBox zbox;
        static Text text;
        static ComboList list;
        static Tree tree;
        static VBox vbox;
        static Button button;
        static PaintArea pa;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Iup.Open();

            //   IUPImage i = new IUPImage("c:/ernie.png");

           
            d = new Dialog(
                vbox = new VBox(
                    button=new Button { Title = "Knapp 1",Action=ButtonPress },
                    new Button { Title = "Knapp 2" },
                    new Button { Title = "Knapp 3" },
                    button = new Button { Title = "Knapp 4" },
                    text=new Text {Expand=Expand.Yes,Action=TextAction},
                    new Button { Title = "Knapp 5" },
                    EditRow("Skriv här:"),
                    EditRow("Skriv här också , lite längre:"),
                    EditRow("Skriv här också , lite längre: fkljdsfl sdjfh lsdkjfh lsdkjf lskdjfh adlfj lsadjfhlaskdj flkdsjah flkjadshlkfsla dfljhdslf "),


                    pa = new PaintArea() { RasterSize = new Size2i(500, 500), Action = PaintCB,ButtonCB=PaintButton },
                    list = new ComboList { EditBox = true }

                 ) { Margin = new Size2i(8, 8),Gap=4, }



              ) { Title = "Test program", KAny = KAnyCB,CloseCB=OnCloseWindow };


            button.Title = "åäöÅÄÖ";
            
            d.ShowXY(DialogPos.Center,DialogPos.Center);


            Matrix2d m = Matrix2d.Rotate(1.2) * Matrix2d.Translate(100, 50.67) * Matrix2d.Stretch(2.4, 6.7) * Matrix2d.Rotate(1.34);

            Matrix2d inv = m.Inverse;

            Matrix2d res = m * inv;


            Iupc.MainLoop();

            Iup.Close();
        }

        static Point2d pt = null;


        public static void ButtonPress(CallbackData e)
        {

            pt = null;

            d.Title = "Pressed";
            while (pt == null)
            {
                Iup.LoopStep();
                Thread.Sleep(1);
            }

            d.Title = "After press";
        }

        public static void PaintButton(CallbackData e)
        {
            if (e.Value == 1) 
                pt = new Point2d(e.X, e.Y);
        }

        public static void TextAction(CallbackData e)
        {
            e.Value = Char.ToUpper((char)e.Value); 
        }

        public static HBox EditRow(string caption)
        {
            return new HBox(
                new Label { Title = caption, Expand = Expand.Yes,Alignment=Alignment.Right },
                new Text { Multiline = false, Size = new Size2i(50, 0) }
            ) { Margin = new Size2i(0, 0),Gap=8,Expand=Expand.Horizontal };
        }

        public static void OnCloseWindow(CallbackData e) {

            
        }

        

        static void KAnyCB(CallbackData e)
        {
            if (e.Key == Key.F1)
            {
                
                Iup.Message("", Iup.Focused.Title);
            }
            else if (e.Key == Key.F2)
            {
                button.NextField();
            }
        }

        static void EditCB(CallbackData e)
        {
            Iupc.Message("", e.Key.ToString());
        }

        static void TextMotionCB(CallbackData e)
        {
            TreeNode tn = tree.ConvertXYToNode(e.X, e.Y);

            if (tn == null)
                d.Title = "No node";
            else
            {
                d.Title = tn.Title;
                tree.Selected = tn;
            }
        }

        static void PaintCB(CallbackData e)
        {
            Canvas c = ((PaintArea)e.Sender).CreateCanvas();

            c.Activate();

            c.Transform = new Matrix2d();

            c.Background = 0;
            c.Clear();
           
            //draw and set window+viewport
            c.Foreground = Color.Red.RGB;
            c.Rect(100, 300, 50, 250);
            c.SetViewport(100, 300, 50, 250);
            c.SetWindow(0.0, 20.0, 0.0, 20.0);
            

            //draw some world stuff
            c.Foreground = Color.Yellow.RGB;
            c.Transform = Matrix2d.Identity;
            c.TranslateW(5, 0);
            c.ScaleW(4.0, 1.0);
            c.RotateW(45.0);
            

            
            c.RectW(-1, 1, -1, 1);
            
            

            //draw some non-world stuff
            c.Foreground = Color.Green.RGB;
            c.Transform = Matrix2d.Identity;
            c.Translate(100, 0);
            c.Scale(4.0, 1.0);
            c.Rotate(45.0);
            
            
            
            

            c.Rect(-10, 10, -10, 10);

        }

        static Point2d World2Win(Canvas c,int x, int y)
        {
            double winx1, winx2, winy1, winy2;
            int vpx1, vpy1, vpx2, vpy2;

            c.GetWindow(out winx1, out winx2, out winy1, out winy2);
            c.GetViewport(out vpx1, out vpx2, out vpy1, out vpy2);

            double sx=(double)(vpx2 - vpx1) / (winx2 - winx1);
            double sy=(double)(vpy2 - vpy1) / (winy2 - winy1);
            //create matrix given window/viewport
            Matrix2d mat = new Matrix2d(
                sx,
                0.0,
                0.0,
                sy,
                vpx1-winx1*sx,
                vpy1-winy1*sy
            );
               
           
            Point2d rp = new Point2d(x, y);
            return rp.Transform(mat);

        }

       
      
    }
}
